//
//  PopUpView.m
//  SNS
//
//  Created by Bobo on 11-12-11.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "PopUpView.h"
#import "QQManager.h"
#import "SinaManager.h"
#import "RenrenManager.h"
#import "HomeController.h"

@implementation PopUpView

@synthesize homeController;

- (void) sinaLogin {
    [self.homeController removePopUpView];
	[[SinaManager sharedManager] login];
}

- (void) sinaLogout {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否退出新浪微博账号？"
                                                    message:@"退出新浪微博账号将无法使用本程序大部分功能！"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确认", nil];
    alert.tag = 0;
    [alert show];
    [alert release];
    
    
}

- (void) renrenLogin {
    [self.homeController removePopUpView];
    [[RenrenManager sharedManager] login];
}

- (void) renrenLogout {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否退出人人账号？"
                                                    message:@"退出人人账号将无法同步信息到人人网!"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确认", nil];
    alert.tag = 1;
    [alert show];
    [alert release];
    
    
}

- (void) qqLogin {
    [self.homeController removePopUpView];
    [[QQManager sharedManager] login];
}

- (void) qqLogout {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否退出腾讯微博账号？"
                                                    message:@"退出腾讯微博账号将无法同步信息到腾讯微博！"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确认", nil];
    alert.tag = 2;
    [alert show];
    [alert release];
    
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        UIButton *sinaButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sinaButton.frame = CGRectMake(5, 10 + 5, 40, 40);
        [self addSubview:sinaButton];
        if ([[SinaManager sharedManager].sinaWeiBo isUserLoggedin]) {
            UIImage *sinaIcon = [UIImage imageNamed:@"sinaIcon.png"];
            [sinaButton setImage:sinaIcon forState:UIControlStateNormal];
            [sinaButton addTarget:self action:@selector(sinaLogout) forControlEvents:UIControlEventTouchUpInside];
        }
        else {
            UIImage *sinaBlackIcon = [UIImage imageNamed:@"sinaIcon_black.png"];
            [sinaButton setImage:sinaBlackIcon forState:UIControlStateNormal];
            [sinaButton addTarget:self action:@selector(sinaLogin) forControlEvents:UIControlEventTouchUpInside];
        }
        
        UIButton *tengxunButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tengxunButton.frame = CGRectMake(5 + 50, 10 + 5, 40, 40);
        [self addSubview:tengxunButton];
        if ([[QQManager sharedManager] isUserLoggedin]) {
            UIImage *tengxunIcon = [UIImage imageNamed:@"tengxunIcon.png"];
            [tengxunButton setImage:tengxunIcon forState:UIControlStateNormal];
            [tengxunButton addTarget:self action:@selector(qqLogout) forControlEvents:UIControlEventTouchUpInside];
        }
        else {
            UIImage *tengxunBlackIcon = [UIImage imageNamed:@"tengxunIcon_black.png"];
            [tengxunButton setImage:tengxunBlackIcon forState:UIControlStateNormal];
            [tengxunButton addTarget:self action:@selector(qqLogin) forControlEvents:UIControlEventTouchUpInside];
        }
        
        UIButton *renrenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        renrenButton.frame = CGRectMake(5 + 100, 10 + 5, 40, 40);
        [self addSubview:renrenButton];
        if ([[RenrenManager sharedManager].renren isSessionValid]) {
            UIImage *renrenIcon = [UIImage imageNamed:@"renrenIcon.png"];
            [renrenButton setImage:renrenIcon forState:UIControlStateNormal];
            [renrenButton addTarget:self action:@selector(renrenLogout) forControlEvents:UIControlEventTouchUpInside];
        }
        else {
            UIImage *renrenBlackIcon = [UIImage imageNamed:@"renrenIcon_black.png"];
            [renrenButton setImage:renrenBlackIcon forState:UIControlStateNormal];
            [renrenButton addTarget:self action:@selector(renrenLogin) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    float radius = 5;
    float arrowWidth = 10;

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 20, arrowWidth);
    CGPathAddArcToPoint(path, NULL, 0, arrowWidth, 0, 50 + arrowWidth, radius);
    CGPathAddArcToPoint(path, NULL, 0, 50 + arrowWidth, 150, 50 + arrowWidth, radius);
    CGPathAddArcToPoint(path, NULL, 150, 50 + arrowWidth, 150, arrowWidth, radius);
    CGPathAddArcToPoint(path, NULL, 150, arrowWidth, 20, arrowWidth, radius);
    CGContextAddPath(context, path);
    
    CGMutablePathRef path2 = CGPathCreateMutable();
    CGPathMoveToPoint(path2, NULL, 20 - arrowWidth/2, arrowWidth);
    CGPathAddLineToPoint(path2, NULL, 20, 0);
    CGPathAddLineToPoint(path2, NULL, 20 + arrowWidth/2, arrowWidth);
    CGPathAddLineToPoint(path2, NULL, 20 - arrowWidth/2, arrowWidth);
    CGContextAddPath(context, path2);
    
    CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0
                                                              alpha:0.5f].CGColor);
    CGContextFillPath(context);
    CGPathRelease(path);
    CGPathRelease(path2);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        if (alertView.tag == 0) {
            [self.homeController removePopUpView];
            [[SinaManager sharedManager] logout];
        }
        else if (alertView.tag == 1) {
            [self.homeController removePopUpView];
            [[RenrenManager sharedManager] logout];
        }
        else if (alertView.tag == 2) {
            [self.homeController removePopUpView];
            [[QQManager sharedManager] logout];
        }
    }
}

@end
